/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights.  These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.  Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/


#ifndef CORRELATION_H
#define CORRELATION_H


#include "CorrelationParent.h"


/*
  Correlation class

  This class provides functions which can store and analyze an image for the digital image correlation procedure. The
  class is meant to be used in the context of a sequence of images taken by a camera in an inertial reference frame.

  Note: Dynamic memory allocation allows for larger arrays. This is because the stack has more memory available than
  the heap.

  Rule: cvPoint(x, y)
     x -> column
     y -> row

  Rule: bw[r][c]
     r -> row
     c -> column

  */
class Correlation : public CorrelationParent {


public:


    /* ----- Level 0 Functions --------------------------------------------------------------------------------------- */

    void initObject();
    bool analyzeImage(string destPathName, string sourcePathName, string fileName, bool makeReferenceImage);
    void storePositionField(string path, bool isFirstRun=false);
    void storeStrainField(string path, bool isFirstRun=false);
    void outputImage(bool showInWindow=true, bool switchIsDot=false);
    void outputOverlay(bool showInWindow=true);


    /* ----- Level 1 Functions --------------------------------------------------------------------------------------- */

    void loadPersistentValues();
    void savePersistentValues();
    bool inputImage(string file);
    void blobAnalysis();
    void extractPositionField(bool isFirstRun=false);
    void generateGridMap();
    void computeStrain();
    void drawCurrentState(bool showInWindow=false, bool switchIsDot=false);


    /* ----- Level 2 Functions --------------------------------------------------------------------------------------- */

    RgbImage subsetImage(RgbImage src);
    void filterImage(IplImage *imgInLocal);
    int matchBlob(int c, int r, int criteria);
    void updateBoundingBox(int blob_track, int c, int r);
    void mergeBlobs(int fromBlob, int toBlob);
    void postProcess();
    void blobProbability();
    void identifyDots();
    bool isValidPointSet(double pt[2][3], double pt_star[2][3], int p, int n1, int n2);
    void localStrain(double pt[2][3], double pt_star[2][3], int count, int method);
    double minAbs(double a, double b);
    double maxAbs(double a, double b);
    double minIgnore(double a, double b);
    double maxIgnore(double a, double b);
    void drawBlobProperty(int whichProperty, int blob_track, int B, int G, int R);
    void drawCurrentPixel(int c, int r, int B, int G, int R);
    void debugHandle(int direction=-1, string functionName="unknown function");





public:

    bool debugParam;
};


#endif // CORRELATION_H
